package querysrc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.subway.babynote.syncdata.*;


public class query_syncfeeds extends query {
	public query_syncfeeds(sqlConnectionHandler sqlconhandler)
	{
		super(sqlconhandler);
	}	
	
	public List<ClientDataRecordBase> get_feeds(int user_id,int ts)
	{
		Connection con = null;	
		ResultSet rs = null;
		querydb qdb = null;
		ArrayList<ClientDataRecordBase> feeds = new ArrayList<ClientDataRecordBase>();
		try
		{
			boolean bret = true;
			con = this.init();	
			qdb = new querydb(con,sqlscripts.sql_feeds_get_latest_select);		
			bret = qdb.prePS();		
			if(bret)
			{
				qdb.ps.setInt(1, user_id);
				qdb.ps.setInt(2, sqlscripts.feeds_max_latest_days);
				qdb.ps.setInt(3, ts);
				rs = qdb.execPSQuery();
				if(rs.next())
				{
					rs.beforeFirst();
					while(rs.next())
					{
						int id = rs.getInt(sqlscripts.sql_feeds_get_latest_select_c1_food_id);
						java.sql.Date r_time = StringUtil.str2date( rs.getString(sqlscripts.sql_feeds_get_latest_select_c2_r_time) );
						float q = rs.getFloat(sqlscripts.sql_feeds_get_latest_select_c3_quantity);
						int t = rs.getInt(sqlscripts.sql_feeds_get_latest_select_c4_ts);
						
						feeds.add(new ClientDataRecord.feedRecord(id,r_time,q,t));												
					}					
				}
			}
			
			return feeds;
		}
		catch(SQLException se) {
		      System.out.println("SQLException: "+se.getMessage());
		      return feeds;
	    }
		catch(Exception e)
		{
			e.printStackTrace();
			return feeds;
		}
		finally
		{
			this.release_resultset(rs);
			this.release_querydb(qdb);
			this.release_connection(con);
			this.finalize();
		}
	}
}
